package ysq.xyg.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.dao.DictDao;
import ysq.xyg.model.Dict;
import ysq.xyg.service.DictService;

import javax.annotation.Resource;
import java.util.List;


/**
 * @author yu sheng qiang
 * @create 2017-03-22 15:36
 **/
@Service("dictService")
public class DictServiceImpl extends BaseServiceImpl<Dict> implements DictService {


    @Resource
    private DictDao dictDao;
    @Autowired
    public void setDictDao(DictDao dictDao) {
        this.baseDao = dictDao;
    }

    @Override
    public PageInfo<Dict> pageInfo(Dict dict, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        // 通用Example查询
        Example example = new Example(Dict.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotEmpty(dict.getName())) {
            criteria.andLike("name", "%" + dict.getName() + "%");
        }
        if (StringUtils.isNotEmpty(dict.getType())) {
            criteria.andEqualTo("type", dict.getType());
        }
        List<Dict> list = dictDao.selectByExample(example);
        return new PageInfo<Dict>(list);
    }
    @Override
    public List<Dict> list(Dict dict) {
        // 通用Example查询
        Example example = new Example(Dict.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotEmpty(dict.getName())) {
            criteria.andLike("name", "%" + dict.getName() + "%");
        }
        if (StringUtils.isNotEmpty(dict.getType())) {
            criteria.andEqualTo("type", dict.getType());
        }
        example.setOrderByClause(" sort asc ");
        List<Dict> list = dictDao.selectByExample(example);
        return list;
    }
}
